package com.citymanage.demo.controller.worker;

import com.citymanage.demo.po.worker.Worker;
import com.citymanage.demo.service.cases.CaseService;
import com.citymanage.demo.service.worker.WorkerManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RequestMapping("/worker")
@Controller
public class WorkerController {
    public Integer userCurrentID;
    @Autowired
    private WorkerManageService workerManageService;
    @Autowired
    private CaseService caseService;
    @RequestMapping("/handlecase" )
    public String showWorkerTableComplete(Model model,Model model1){
        model.addAttribute("workers",workerManageService.findAll(userCurrentID));
        model1.addAttribute("cases",caseService.selectAll());
        return "worker/handlecase";
    }
    @RequestMapping("/change_selfinfo")
    public String showWorkerChangeSelfInfo(Model model){

        model.addAttribute("workers",workerManageService.findAll(userCurrentID));
        return "worker/change_selfinfo";}





//查询个人信息
    @RequestMapping("")
    public String showWorkerHome(HttpServletRequest request, Model model){
        final Map map = model.asMap();
        for (final Object obj : map.keySet()) {
            System.out.println(obj.toString() + "==" + map.get(obj));
            userCurrentID=(Integer) map.get(obj);
            model.addAttribute("workers",workerManageService.findAll(userCurrentID));
        }
        return "worker/worker";
    }
    //更新个人信息
    @RequestMapping(value = "/updateWorker", method = RequestMethod.POST)
    public String updateWorker(@RequestParam("account") String account, @RequestParam("phone") String phone, @RequestParam("password1") String password1, @RequestParam("password2") String password2, HttpSession session,Model model) {
        if (password1.equals(password2)) {
            Worker worker = workerManageService.updateWorker(userCurrentID,account , phone, password1);
            if (worker != null) {
                return "index";
            } else return "index";
        } else {

            return "index";
        }
    }

    @RequestMapping("/homePage")
    public String showhomePage(Model model){
        model.addAttribute("workers",workerManageService.workerScore());
        return "/worker/homePage";
    }

    @RequestMapping("/info")
    public String selectPersonal(Model model){
        model.addAttribute("workers", workerManageService.findAll(userCurrentID));
        return "worker/info";
    }

    @RequestMapping(value = "/solveCase" ,method = RequestMethod.POST)
    public String solveCase(@RequestParam("cimg") String cimg,@RequestParam("crescult") String crescult,@RequestParam("ID") String id){
        String absolute="/assets/img/";
        cimg=absolute.concat(cimg);
        Integer cid=Integer.valueOf(id);
        caseService.solveCase(cimg,crescult,userCurrentID,cid);
        return "redirect:/worker/handlecase";
    }
}
